#include "modbuswig.h"
#include "ui_modbuswig.h"

#define TOPIC_DATA_UP       "/app/data/up"//数据接收up modbus
#define TOPIC_DATA_DOWN     "/app/data/down"//命令控制down
#define TOPIC_CTL_UP        "/app/control/up"//数据接收up stm32
#define TOPIC_CTL_DOWN      "/app/control/down"//命令控制down

//通过客户端控制modbus slave
ModbusWig::ModbusWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModbusWig)
{
    mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress("192.168.50.66"));//与mqtt服务器进行连接
    mqtt->setPort(1883);
    mqtt->connectToHost();

    ui->setupUi(this);
    groupopen = new QButtonGroup(this);//开
    groupclose = new QButtonGroup(this);//关

    groupopen->addButton(ui->pushButton_airopen,1);
    groupopen->addButton(ui->pushButton_windopen,2);//将两个个设备的开放到同一按钮组中，通过id方便发送消息

    groupclose->addButton(ui->pushButton_airclose,3);
    groupclose->addButton(ui->pushButton_windclose,4);//将两个设备的关放到同一按钮组，通过id方便发送消息

    connect(groupopen,SIGNAL(buttonClicked(int)),this,SLOT(btnclickopenSlot(int)));
    connect(groupclose,SIGNAL(buttonClicked(int)),this,SLOT(btnclickcloseSlot(int)));

    //给lable设置背景，方便背景前面添加字体
    QString pathariopen = ":/new/prefix1/picture_tubiao/airjinghuaopen.png";
    QString pathariclose = ":/new/prefix1/picture_tubiao/airjinghuaclose.png";

    QString pathwindopen = ":/new/prefix1/picture_tubiao/fengshanopen.png";
    QString pathwindclose = ":/new/prefix1/picture_tubiao/fengshanclose.png";

    //QPixmap airopen;
    airopen.load(pathariopen);

    //QPixmap airclose;
    airclose.load(pathariclose);

    //QPixmap windopen;
    windopen.load(pathwindopen);

    //QPixmap windclose;
    windclose.load(pathwindclose);
}

ModbusWig::~ModbusWig()
{
    delete ui;
}

//设置背景图片
void ModbusWig::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix(":/new/prefix2/picture_tubiao/beijing2.png");
    painter.drawPixmap(0, 0, pix);
}

//数据发送函数
void ModbusWig::publish(QByteArray text)
{
    QMQTT::Message message;
    message.setTopic(TOPIC_DATA_DOWN);//确定下发数据的主题
    message.setPayload(text);//确定下发数据的的内容

    qDebug() << "topic:" <<message.topic();
    qDebug() << "payload:"<<message.payload();

    emit dataDownSignal(message);
}

//数据接收槽函数
void ModbusWig::dataUpSlot(const QByteArray& json)
{
    qDebug()<<"modbus处理解析json语句";
    QJsonParseError jsonerror;//报告 JSON 处理过程中出现的错误
    //将json解析为utf-8的QJSON文档（读写 JSON 文档）
    QJsonDocument doucment = QJsonDocument::fromJson(json,&jsonerror);
    //判断文档是否有效性
    if (!doucment.isNull() && (jsonerror.error == QJsonParseError::NoError))
    {
        //查询一个文档是否包含了一个object
        if(doucment.isObject())
        {
            QJsonObject object = doucment.object();//封装对象
            if(object.contains("type")&&object.contains("result"))
            {
                int type = object.value("type").toInt();
                //qDebug()<<"1-采集回复指令";
                //qDebug()<<"type: "<< type;
                if(object.contains("data"))
                {
                    QJsonValue value= object.value("data");
                    if(value.isArray())
                    {
                        QJsonArray array = value.toArray();
                        qDebug() << "data:" << array.size();
                        for(int i = 0; i < array.size(); i++)
                        {
                            //遍历数组
                            QJsonValue value = array.at(i);
                            if(value.isObject())
                            {
                                object = value.toObject();
                                int key = object.value("key").toInt();
                                QString val = object.value("val").toString();
                                if(key==101)
                                {
                                    FLOAT val1 = val.toFloat();
                                    ui->lcdNumber_TVOC->display(val1);//此处为TVOC的值
                                    //qDebug() << val1 ;
                                    if(val1>0.035)
                                    {
                                        ui->lcdNumber_weixain->display("A");
                                    }
                                    else if(val1>0.015 && val1<0.035)
                                    {
                                        ui->lcdNumber_weixain->display("B");
                                    }
                                    else if(val1<0.015)
                                    {
                                        ui->lcdNumber_weixain->display("C");
                                    }
                                }
                                else if(key==102)
                                {
                                    FLOAT val2 = val.toFloat();
                                    //qDebug() << "102"<<val2 ;
                                    ui->lcdNumber_jiaquan->display(val2);//此处为甲醛的值
                                }
                                else if(key==103)
                                {
                                    int val1 = val.toInt();

                                    if(val1 == 1)//风扇开
                                    {
                                        //ui->pushButton_windopen->setEnabled(false);
                                        ui->label_xiaoxikuang->clear();
                                        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
                                        ui->label_xiaoxikuang->setText(" 风扇已打开");

                                        ui->label_wind->clear();
                                        ui->label_wind->setPixmap(windopen);//显示风扇开

                                    }
                                    else if(val1 == 0)//风扇关
                                    {
                                        ui->label_xiaoxikuang->clear();
                                        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
                                        ui->label_xiaoxikuang->setText(" 风扇已关闭");

                                        ui->label_wind->clear();
                                        ui->label_wind->setPixmap(windclose);//显示风扇开
                                    }
                                }
                                else if(key==104)
                                {
                                    int val1 = val.toInt();
                                    if(val1 == 1)//空气净化器开
                                    {
                                        //ui->pushButton_airopen->setEnabled(false);
                                        ui->label_xiaoxikuang->clear();
                                        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
                                        ui->label_xiaoxikuang->setText(" 空气净化器已打开");

                                        ui->label_air->clear();
                                        ui->label_air->setPixmap(airopen);//显示空气净化器开


                                    }
                                    else if(val1 == 0)//空气净化器关
                                    {
                                        ui->label_xiaoxikuang->clear();
                                        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
                                        ui->label_xiaoxikuang->setText(" 空气净化器已关闭");

                                        ui->label_air->clear();
                                        ui->label_air->setPixmap(airclose);//显示空气净化器关


                                    }
                                }
                                qDebug() << "key:"<< key <<"val:" << val;
                            }
                        }
                    }
                }
            }
        }
    }
}

//空气净化器开按钮
void ModbusWig::btnclickopenSlot(int id)
{
    QJsonObject json;//创建一个空的 QJsonObject 对象,封装json对象
    QJsonDocument document;//读写json文档
    if(id == 1)
    {
        //给lable设置背景图片
        ui->label_air->clear();
        ui->label_air->setPixmap(airopen);//显示空气净化器开

        //背景图片上面添加字
        ui->label_xiaoxikuang->clear();
        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
        ui->label_xiaoxikuang->setText(" 空气净化器已打开");
        
        //构建json序列化
        json.insert("type",2);
        QJsonObject data;
        data.insert("key",104);
        data.insert("val","1");
        json.insert("data",QJsonObject(data));

        document.setObject(json);//在QJsonDocument中创建一个与json相关联的json文档
        QByteArray byte_array = document.toJson(QJsonDocument::Compact);
        //qDebug() << "空气净化器开";
        publish(byte_array);
    }
    else if(id == 2)
    {
        ui->label_wind->clear();
        ui->label_wind->setPixmap(windopen);//显示风扇开

        ui->label_xiaoxikuang->clear();
        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
        ui->label_xiaoxikuang->setText(" 风扇已打开");
        
        json.insert("type",2);
        QJsonObject data;
        data.insert("key",103);
        data.insert("val","1");
        json.insert("data",QJsonObject(data));

        document.setObject(json);//在QJsonDocument中创建一个与json相关联的json文档
        QByteArray byte_array = document.toJson(QJsonDocument::Compact);
        publish(byte_array);
        //qDebug() << "风扇已打开";
    }
}

void ModbusWig::btnclickcloseSlot(int id)
{
    QJsonObject json;//创建一个空的 QJsonObject 对象,封装json对象
    QJsonDocument document;//读写json文档
    if(id == 3)
    {
        ui->label_air->clear();
        ui->label_air->setPixmap(airclose);//显示空气净化器关

        ui->label_xiaoxikuang->clear();
        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
        ui->label_xiaoxikuang->setText(" 空气净化器已关闭");
        json.insert("type",2);
        QJsonObject data;
        data.insert("key",104);
        data.insert("val","0");
        json.insert("data",QJsonObject(data));

        document.setObject(json);//在QJsonDocument中创建一个与json相关联的json文档
        QByteArray byte_array = document.toJson(QJsonDocument::Compact);

        publish(byte_array);
        //qDebug() << "空气净化器已关闭";
    }
    else if(id == 4)
    {
        ui->label_wind->clear();
        ui->label_wind->setPixmap(windclose);//显示风扇关

        ui->label_xiaoxikuang->clear();
        ui->label_xiaoxikuang->setStyleSheet(QString("border-image:url(:/new/prefix1/picture_tubiao/speak.png);"));
        ui->label_xiaoxikuang->setText(" 风扇已关闭");
        
        json.insert("type",2);
        QJsonObject data;
        data.insert("key",103);
        data.insert("val","0");
        json.insert("data",QJsonObject(data));

        document.setObject(json);//在QJsonDocument中创建一个与json相关联的json文档
        QByteArray byte_array = document.toJson(QJsonDocument::Compact);

        publish(byte_array);

        //qDebug() << "风扇已关闭";
    }
}
